package com.jh.netty.server;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Arrays;

/**
 * 自定义处理器，用来将bytebuf转成我们需要的byte[]。
 * 服务端解析客户端发送的数据
 */
public class BytebufToByteHandler extends SimpleChannelInboundHandler<ByteBuf> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        System.out.println("==ByteBuf 转字节======>>>>>>>: " + ByteBufUtil.hexDump(msg));
        byte[] contentBytes = new byte[msg.readableBytes()];
        msg.readBytes(contentBytes);

        System.out.println("==字节转数组======>>>>>>>: " + Arrays.toString(contentBytes));
        ctx.fireChannelRead(contentBytes);
    }
}
